package com.appcenter.application.service;

import com.appcenter.application.dto.datamarks.DataMarkDto;
import com.appcenter.application.mapper.DataMarksMapper;
import com.appcenter.application.model.DataMarks;
import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.unit.ObjectUnit;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.UUID;
@Service
public class DataMarkService {
    @Autowired
    DataMarksMapper dataMarksMapper;
//    @Transactional
    public DataMarkDto OperatDataMark(DataMarkDto input) throws Exception {
        DataMarkDto output = new DataMarkDto();
        Example example = new Example(DataMarks.class);
        Example.Criteria criteriau = example.createCriteria();
        criteriau.andEqualTo("genre", input.getGenre());
        int DataMarkGenreCount=dataMarksMapper.selectCountByExample(example);
        if(DataMarkGenreCount==0){
            //添加
            DataMarks curDataMarks=new DataMarks();
            String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            curDataMarks.setId(uuid);
            curDataMarks.setGenre(input.getGenre());
            curDataMarks.setLastTime(input.getLastTime());
            curDataMarks.setValidStatus(true);
            BusinessLogger.Info(LoggerGenre.Add,"添加数据标识",input);
            dataMarksMapper.insertSelective(curDataMarks);
            BeanUtils.copyProperties(curDataMarks,output);
        }else{
            //更新
            Example oldexample = new Example(DataMarks.class);
            Example.Criteria oldcriteriau = oldexample.createCriteria();
            oldcriteriau.andEqualTo("genre", input.getGenre());
            DataMarks oldDataMarks=dataMarksMapper.selectOneByExample(oldexample);
            oldDataMarks.setLastTime(input.getLastTime());
            BusinessLogger.Info(LoggerGenre.Update,"修改数据标识",input);
            dataMarksMapper.updateByPrimaryKey(oldDataMarks);
            BeanUtils.copyProperties(oldDataMarks,output);
        }
        return output;
    }

    public DataMarkDto GetOperatDataMark(String Genre) throws Exception {
        DataMarkDto output = new DataMarkDto();
        Example example = new Example(DataMarks.class);
        Example.Criteria criteriau = example.createCriteria();
        criteriau.andEqualTo("genre", Genre);
        criteriau.andEqualTo("validStatus",true);
        BusinessLogger.Info(LoggerGenre.View,"获取数据标识",Genre);
        DataMarks curDataMarks=dataMarksMapper.selectOneByExample(example);
        BeanUtils.copyProperties(curDataMarks,output);
        return output;
    }





}
